package com.neusoft.EYSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neusoft.EYSystem.dao.MealitemDAO;
import com.neusoft.EYSystem.po.Mealitem;
import com.neusoft.EYSystem.po.NursingProject;
import com.neusoft.EYSystem.po.PageResponseBean;
import com.neusoft.EYSystem.po.ResponseBean;
import com.neusoft.EYSystem.service.MealitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mealitem")
public class MealitemController {
    @Autowired
    private MealitemService mealitemService;
    @Autowired
    private MealitemDAO mealitemDAO;

    //管理员端：膳食管理 膳食基础信息管理 显示膳食信息表
    // 支持根据膳食名查询
    @GetMapping("/pageMealitem")
    public PageResponseBean<List<Mealitem>> pageMealitem(Long cur, Long number, Mealitem mealitem) {
        return mealitemService.pageMealitem(cur, number, mealitem);
    }

    // 管理员端 膳食日历 修改膳食日历/新增膳食日历 膳食名下拉框
    @PostMapping("/getMealitem")
    public List<Mealitem> getMealitem() {
        QueryWrapper<Mealitem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1); // 只获取启用的膳食项
        return mealitemDAO.selectList(queryWrapper);
    }

    //管理员端：膳食管理 膳食基础信息管理 添加膳食信息
    @PostMapping("/addMealitem")
    public ResponseBean<Integer> addMealitem(Mealitem mealitem) {
        ResponseBean<Integer> response = new ResponseBean<>();
        mealitem.setStatus("1");
        try {
            if(mealitemDAO.insert(mealitem) > 0){
                response.setStatus(200);
                response.setMsg("膳食品类添加成功");
                return response;
            }else {
                response.setStatus(400);
                response.setMsg("膳食品类添加失败，可能是数据不完整或重复");
            }
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("膳食品类添加失败: " + e.getMessage());
        }
        return response;
    }

    //管理员端：膳食管理 膳食基础信息管理 删除膳食信息
    @PostMapping("/deleteMealitem")
    public ResponseBean<Integer> deleteMealitem(Mealitem mealitem) {
        Integer result;
        ResponseBean<Integer> rb = null;
        try {
            result = mealitemService.deleteStatusById(mealitem);
            rb =new ResponseBean<>(result);

        } catch (Exception e) {
            e.printStackTrace();
            rb =new ResponseBean<>(500,"删除膳食品类错误");
        }
        return rb;
    }

    //管理员端：膳食管理 膳食基础信息管理 修改膳食信息
    @PostMapping("/updateMealitem")
    public ResponseBean<Integer> updateMealitem(Mealitem mealitem) {
        Integer result;
        ResponseBean<Integer> rb = null;
        UpdateWrapper<Mealitem> uw = new UpdateWrapper<>();
        uw.eq("mealitem_id", mealitem.getMealitemId());
        try{
            result = mealitemDAO.update(mealitem, uw);
            rb = new ResponseBean<>(result);
        }catch (Exception e){
            e.printStackTrace();
            rb =new ResponseBean<>(500,"修改膳食品类错误");
        }
        return rb;
    }
}
